package cn.javaxc.mvcwork.modules.file;
import cn.javaxc.mvcwork.controller.*;
import cn.javaxc.mvcwork.controller.web.*;
import cn.javaxc.model.base.*;
import cn.javaxc.mvcwork.util.helper.FrameHelper;
import cn.javaxc.mvcwork.util.common.*;
import java.sql.*;
import java.util.*;




public class FileItemList extends ArrayList{
  public final static String uniqueField = "item_id";
  private java.sql.Timestamp lastAnchor = null;
  private java.sql.Timestamp nextAnchor = null;
  private int syncType = MVCConstant.NO_SYNC;

  //filters

  private String linkModuleId = null;
  private Object linkItemId = -1;
  private int folderId = -1;
  private int owner = -1;
  private String itemIdRange = null;
  private String fileLibraryPath = null;
  private boolean topLevelOnly = false;
  private boolean webImageFormatOnly = false;
  private int defaultFile = MVCConstant.UNDEFINED;
  private int enabled = MVCConstant.UNDEFINED;
  //calendar
  protected java.sql.Timestamp alertRangeStart = null;
  protected java.sql.Timestamp alertRangeEnd = null;

  //html select
  private String htmlJsEvent = "";
  private int buildPortalRecords = MVCConstant.UNDEFINED;

  
  public FileItemList() {
  }

  

  
  public String getUniqueField() {
    return uniqueField;
  }

  
  public void setLastAnchor(Timestamp lastAnchor) {
    this.lastAnchor = lastAnchor;
  }

  
  public void setLastAnchor(String lastAnchor) {
    this.lastAnchor = java.sql.Timestamp.valueOf(lastAnchor);
  }

  
  public void setNextAnchor(Timestamp nextAnchor) {
    this.nextAnchor = nextAnchor;
  }

  
  public void setNextAnchor(String nextAnchor) {
    this.nextAnchor = java.sql.Timestamp.valueOf(nextAnchor);
  }

  
  public void setSyncType(int syncType) {
    this.syncType = syncType;
  }

  
  public void setSyncType(String syncType) {
    this.syncType = Integer.parseInt(syncType);
  }


   
  public int getEnabled() {
    return enabled;
  }


  
  public void setEnabled(int tmp) {
    this.enabled = tmp;
  }


  
  public void setEnabled(String tmp) {
    this.enabled = Integer.parseInt(tmp);
  }


  
  public String getHtmlJsEvent() {
    return htmlJsEvent;
  }


  
  public void setHtmlJsEvent(String tmp) {
    this.htmlJsEvent = tmp;
  }

  
  public int getDefaultFile() {
    return defaultFile;
  }


  
  public void setDefaultFile(int tmp) {
    this.defaultFile = tmp;
  }


  
  public void setDefaultFile(String tmp) {
    this.defaultFile = Integer.parseInt(tmp);
  }


  public void setLinkModuleId(Object tmp) {
	  if (!StringUtils.isNullData(tmp)){
		linkModuleId = tmp+"";  
	  }
    
  }

  
  public String getLinkModuleId() {
    return linkModuleId;
  }


  
  public void setLinkItemId(Object tmp) {
    this.linkItemId = tmp;
  }


  
  public Object getLinkItemId() {
    return linkItemId;
  }




  
  public void setOwner(int tmp) {
    this.owner = tmp;
  }


  
  public void setItemIdRange(String tmp) {
    this.itemIdRange = tmp;
  }


  
  public void setFolderId(int tmp) {
    this.folderId = tmp;
  }


  
  public void setFileLibraryPath(String tmp) {
    this.fileLibraryPath = tmp;
  }


  
  public void setTopLevelOnly(boolean tmp) {
    this.topLevelOnly = tmp;
  }


  
  public boolean getWebImageFormatOnly() {
    return webImageFormatOnly;
  }


  
  public void setWebImageFormatOnly(boolean tmp) {
    this.webImageFormatOnly = tmp;
  }


  
  public void setWebImageFormatOnly(String tmp) {
    this.webImageFormatOnly = DatabaseUtils.parseBoolean(tmp);
  }


  
  public void setAlertRangeStart(java.sql.Timestamp tmp) {
    this.alertRangeStart = tmp;
  }


  
  public void setAlertRangeStart(String tmp) {
    this.alertRangeStart = DatabaseUtils.parseTimestamp(tmp);
  }


  
  public void setAlertRangeEnd(java.sql.Timestamp tmp) {
    this.alertRangeEnd = tmp;
  }


  
  public void setAlertRangeEnd(String tmp) {
    this.alertRangeEnd = DatabaseUtils.parseTimestamp(tmp);
  }





  
  public int getOwner() {
    return owner;
  }


  
  public String getItemIdRange() {
    return itemIdRange;
  }


  
  public int getFolderId() {
    return folderId;
  }


  
  public String getFileLibraryPath() {
    return fileLibraryPath;
  }


  
  public long getFileSize() {
    long fileSize = 0;
    Iterator i = this.iterator();
    while (i.hasNext()) {
      fileSize += ((FileItem) i.next()).getSize();
    }
    return fileSize;
  }


  
  public java.sql.Timestamp getAlertRangeStart() {
    return alertRangeStart;
  }


  
  public java.sql.Timestamp getAlertRangeEnd() {
    return alertRangeEnd;
  }


 


  
  public int getBuildPortalRecords() {
    return buildPortalRecords;
  }


  
  public void setBuildPortalRecords(int buildPortalRecords) {
    this.buildPortalRecords = buildPortalRecords;
  }

  
  
  public SQLMap prepareList( String sqlFilter, String sqlOrder)  {
    StringBuffer sqlSelect = new StringBuffer();

    //Build a base SQL statement for returning records
      sqlSelect.append("SELECT ");
    sqlSelect.append(
        "f.*, t.filename AS thumbnail " +
            "FROM ts_files f " +
            "LEFT JOIN ts_files_thumbnail t ON (f.item_id = t.item_id AND f.version = t.version) " +
            "WHERE f.item_id > -1 ");
    if(sqlFilter == null || sqlFilter.length() == 0){
      StringBuffer buff = new StringBuffer();
      createFilter( buff);
      sqlFilter = buff.toString();
    }
    SQLMap pst = FrameHelper.getBaseSQL().createStatement(sqlSelect.toString() + sqlFilter + sqlOrder);//.log(new SQLLog());
    prepareFilter(pst);
    return pst;
  }

   
  public void buildList()  {
    int items = -1;
    StringBuffer sqlFilter = new StringBuffer();
    StringBuffer sqlOrder = new StringBuffer();
    createFilter( sqlFilter);
    {
      sqlOrder.append("ORDER BY f.subject ");
    }

    SQLMap pst = prepareList(sqlFilter.toString(), sqlOrder.toString());
	
    SQLList<SQLMap> elist= pst.executeQuery(null,null);
	for (SQLMap rs:elist){
      FileItem thisItem = new FileItem(rs);
      thisItem.setDirectory(fileLibraryPath);
      this.add(thisItem);	
	}
  }


  
  public void delete( String baseFilePath)  {
    Iterator documents = this.iterator();
    while (documents.hasNext()) {
      FileItem thisFile = (FileItem) documents.next();
      thisFile.delete( baseFilePath);
    }
  }


  
  private void createFilter( StringBuffer sqlFilter) {
    if (sqlFilter == null) {
      sqlFilter = new StringBuffer();
    }
    if (!StringUtils.isNullData(linkModuleId)) {
      sqlFilter.append("AND f.link_module_id = ? ");
    }
    if (SQL.isNotNull(linkItemId)) {
      sqlFilter.append("AND f.link_item_id = ? ");
    }
    if (folderId > -1) {
      sqlFilter.append("AND f.folder_id = ? ");
    }
    if (owner != -1) {
      sqlFilter.append("AND f.enteredby = ? ");
    }
    if (!StringUtils.isNullData(itemIdRange)) {
      sqlFilter.append("AND f.item_id IN (" + itemIdRange + ") ");
    }
    if (topLevelOnly) {
      sqlFilter.append("AND f.folder_id IS NULL ");
    }
    if (buildPortalRecords != MVCConstant.UNDEFINED) {
      sqlFilter.append("AND f.allow_portal_access = ? ");
    }
    if (webImageFormatOnly) {
      sqlFilter.append(
          "AND (f.client_filename LIKE '%.gif' OR f.client_filename LIKE '%.jpg' OR f.client_filename LIKE '%.png') ");
    }
    if (alertRangeStart != null) {
      sqlFilter.append("AND f.modified >= ? ");
    }
    if (alertRangeEnd != null) {
      sqlFilter.append("AND f.modified < ? ");
    }
    if (defaultFile != MVCConstant.UNDEFINED) {
      sqlFilter.append("AND f.default_file = ? ");
    }
    if (enabled != MVCConstant.UNDEFINED) {
      sqlFilter.append("AND f.enabled = ? ");
    }
  }


  
  protected int prepareFilter(SQLMap pst)  {
    int i = 0;
	if (!StringUtils.isNullData(linkModuleId)) {
      pst.setString(++i, linkModuleId);
    }
    if (SQL.isNotNull(linkItemId)) {
      pst.setObject(++i, linkItemId);
    }
    if (folderId > -1) {
      pst.setInt(++i, folderId);
    }
    if (owner != -1) {
      pst.setInt(++i, owner);
    }
    if (alertRangeStart != null) {
      pst.setTimestamp(++i, alertRangeStart);
    }
    if (alertRangeEnd != null) {
      pst.setTimestamp(++i, alertRangeEnd);
    }
    if (buildPortalRecords != MVCConstant.UNDEFINED) {
      pst.setBoolean(++i, buildPortalRecords == MVCConstant.TRUE);
    }
    if (defaultFile != MVCConstant.UNDEFINED) {
      pst.setBoolean(++i, defaultFile == MVCConstant.TRUE);
    }
    if (enabled != MVCConstant.UNDEFINED) {
      pst.setBoolean(++i, enabled == MVCConstant.TRUE);
    }
    return i;
  }


  



}
